package cn.edu.nju.software.gui;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import cn.edu.nju.software.net.Connection;

public class ResetCode extends JPanel implements ActionListener{
	 
	Connection connection; 
	StudentsOperation so;
	String oldPassword;
	String ensure;
	String newPassword;
	public ResetCode(StudentsOperation soIn){
		super();
		so = soIn;
		connection = Connection.getInstance();
		
		
		JPanel textPanel = new JPanel();
		JPanel buttonPanel = new JPanel();
		GridLayout gridLayout = new GridLayout(3,2);
		BorderLayout borderLayout = new BorderLayout();
		textPanel.setLayout(gridLayout);//�޸����벿����gridlayout��
		JLabel oldCode = new JLabel("Old Code:");
		final JPasswordField oldCodeValue = new JPasswordField(20);
		JLabel newCode = new JLabel("New Code:");
		final JPasswordField newCodeValue = new JPasswordField(20);
		JLabel ensureNewCode = new JLabel("New Code Again:");
		final JPasswordField ensureNewCodeValue = new JPasswordField(20);
		textPanel.add(oldCode);
		textPanel.add(oldCodeValue);
		textPanel.add(newCode);
		textPanel.add(newCodeValue);
		textPanel.add(ensureNewCode);
		textPanel.add(ensureNewCodeValue);
		JButton button = new JButton("submit");
		
		button.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent e){
				ensure = ensureNewCodeValue.getText();
				newPassword = newCodeValue.getText();
				oldPassword = oldCodeValue.getText(); 
				
				if(newPassword.equals(ensure)){
					
				   if(connection.handleChangePassword(so.getUsername(), oldPassword, newPassword)){
					JOptionPane.showMessageDialog(null, "Reset successfully");
				     }
				   else{
					JOptionPane.showMessageDialog(null, "Fail to Reset");
				    }
				}
				// TODO Auto-generated method stub
				else{
					JOptionPane.showMessageDialog(null, "New Code is not the same with New Code Again");
					}
				
				oldCodeValue.setText("");
				newCodeValue.setText("");
				ensureNewCodeValue.setText("");
				                                      }
			
		});
		
		buttonPanel.add(button);
		setLayout(borderLayout);//�����panel��borderlayout��
		add(textPanel,borderLayout.NORTH);
		add(buttonPanel,borderLayout.CENTER);
		
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}
	

}
